package com.dinadi.yidi.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dinadi.yidi.R;
import com.dinadi.yidi.bean.TotalDesignerRequestEntity;
import com.dinadi.yidi.utils.ImageUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by
 * on 2017/7/2.
 */
public class TotalDesignerAdapter extends RecyclerView.Adapter<TotalDesignerAdapter.ViewHolder> implements View.OnClickListener {

    private Context mContext;
    private List<TotalDesignerRequestEntity> dataList = new ArrayList<>();
    private String userId;
    /*运用典型的观察者模式添加点击事件*/
    /*定义接口变量*/
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;


    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, String data);
    }

    public void addAllData(List<TotalDesignerRequestEntity> dataList) {
        this.dataList.addAll(dataList);
        notifyDataSetChanged();
    }

    public void clearData() {
        this.dataList.clear();
    }

    public TotalDesignerAdapter(Context context) {
        mContext = context;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView designer_name, designer_brand_name,tv_signatures;
        ImageView designer_img, designer_vip;

        public ViewHolder(View itemView) {
            super(itemView);
            designer_name = (TextView) itemView.findViewById(R.id.designer_name);
            designer_brand_name = (TextView) itemView.findViewById(R.id.designer_brand_name);
            designer_img = (ImageView) itemView.findViewById(R.id.designer_img);
//            designer_vip = (ImageView) itemView.findViewById(R.id.designer_vip);
            tv_signatures = (TextView) itemView.findViewById(R.id.tv_signatures);
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_total_designer_item, parent, false);

        //将创建的View注册点击事件
        v.setOnClickListener(this);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.designer_name.setText(dataList.get(position).getNickName());
        holder.designer_brand_name.setText(dataList.get(position).getBrandName());
        holder.tv_signatures.setText(dataList.get(position).getSignature());
//        String lable = dataList.get(position).getLable();
//        if (lable.equals("3")) {
//            /*认证*/
//            holder.designer_vip.setImageResource(R.drawable.designer_brand_icon_authentication);
//        } else if (lable.equals("1")) {
//            /*推荐 */
//            holder.designer_vip.setImageResource(R.drawable.designer_brand_icon_recommend);
//        } else if (lable.equals("2")) {
//            /*中国样 */
//            holder.designer_vip.setImageResource(R.drawable.designer_brand_icon_zhongguoyang);
//        }
        String imgUrl = dataList.get(position).getHeadImg();
        if (imgUrl != null) {
            if (!imgUrl.equals("")) {
                ImageUtils.loadImageView(mContext, imgUrl, holder.designer_img);
            } else {
                ImageUtils.loadImageView(mContext, R.drawable.symbols_bitmap_head_portrait, holder.designer_img);
            }
        } else {
            ImageUtils.loadImageView(mContext, R.drawable.symbols_bitmap_head_portrait, holder.designer_img);
        }
        userId = dataList.get(position).getUserId();
        holder.itemView.setTag(userId);

    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    /*将事件转移给外面的调用者*/
    @Override
    public void onClick(View v) {

        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(v, (String) v.getTag());

        }
    }

    /*最后暴露给外面的调用者*/
    public void setmOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;

    }
}